package com.example.lancooznbk.presenter;

import com.example.lancooznbk.model.api.BaseReviewApi;
import com.example.lancooznbk.model.result.BaseReviewItemEntity;
import com.example.lancooznbk.presenter.presenter_interface.IBaseReviewPresenter;
import com.example.lancooznbk.view.base.BaseFragment;
import com.example.lancooznbk.view.base.ILoadMoreDataView;
import com.example.lancooznbk.view.fragment.BaseReviewFragment;
import com.lancoo.rxretrofitlibrary.Api.UserInfo;
import com.lancoo.rxretrofitlibrary.http.HttpManager;
import com.lancoo.rxretrofitlibrary.listener.HttpOnNextListener;
import com.lancoo.rxretrofitlibrary.subscribers.ProgressSubscriber;

import testPaperJBFX.BasicReviewPaperActivity;

/**
 * Created by tangminglong on 17/7/28.
 */

public class BaseReviewPresenter implements IBaseReviewPresenter {

    private int lastPosition = 0;
    private ILoadMoreDataView view;
    private int paperStatus = -1;//记录当前刷新列表对应筛选项
    private int pageIndex = 1;
    private HttpOnNextListener<BaseReviewItemEntity> httpOnNextListener = new HttpOnNextListener<BaseReviewItemEntity>() {
        @Override
        public void onNext(BaseReviewItemEntity baseReviewItemEntity) {
            if (baseReviewItemEntity.getTotalCount() > 0) {
                view.hideLoading();
                if (pageIndex == 1) {
                    view.loadData(baseReviewItemEntity);
                    lastPosition = 0;
                }else {
                    view.loadMoreData(baseReviewItemEntity);
                }

            }else {
                view.showNoData("暂无基本复习内容");
            }
        }

        @Override
        public void onError(Throwable e) {
            super.onError(e);
            view.showNetError();
        }
    };

    private ProgressSubscriber mSubscriber;

    public BaseReviewPresenter(ILoadMoreDataView view) {
        this.view = view;
    }

    private void getData(){
        if (mSubscriber != null && !mSubscriber.isUnsubscribed()){
            mSubscriber.unsubscribe();
        }
        BaseReviewApi api = new BaseReviewApi(httpOnNextListener, (BaseReviewFragment)view);
        api.setPaperForm("0");
        api.setStatus(paperStatus);
        api.setPageIndex(pageIndex);
        api.setPageSize(lastPosition + 10);
        mSubscriber = new ProgressSubscriber(api);
        HttpManager.getInstance().doHttpDeal(api,mSubscriber);
    }

    @Override
    public void getData(boolean isShowLoading) {
        if (isShowLoading) view.showLoading();
        pageIndex = 1;
        getData();
    }

    @Override
    public void getMoreData() {
        pageIndex = pageIndex + 1;
        getData();
    }

    @Override
    public void getDataByStatus(int statusId) {
        if (paperStatus == statusId) return;
        paperStatus = statusId;
        getData(false);
    }


    public void openItemDetail(int position, BaseReviewItemEntity.BaseReviewItemBean baseReviewItemBean) {
        lastPosition = position;
        UserInfo.PaperStatus = baseReviewItemBean.getState();
        UserInfo.PaperFrom = baseReviewItemBean.getPaperFrom();
        //TODO 调用下面方法进行跳转
//        PaperInfor.ModelFrom = 0;//普通联系
        BasicReviewPaperActivity.launch((BaseFragment) view,baseReviewItemBean, PaperDef.FROM_BASE_REVIEW);
    }
}
